package com.sinosoft.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.base.BaseFormController;
import com.sinosoft.base.Constants;
import com.sinosoft.quick.Quick;
import com.sinosoft.quick.QuickManager;
import com.sinosoft.util.DisplayGetPage;
import com.sinosoft.util.SystemConfigUtil;

@Controller
public class QuickController extends BaseFormController {

	QuickManager qmgr;
	
	@Autowired
	public void setQuickManager(QuickManager qmgr)
	{
		this.qmgr = qmgr;
	}
	
	//快试图列表的翻页
	@RequestMapping("/quick/queryquick.html")
	public ModelAndView showQuickList(Quick quick,Model model,HttpServletRequest request)
	{
		List<Quick> quicklist = null;
		String page =  DisplayGetPage.getPageParamName("quicks",request);//第几页
		HttpSession session = request.getSession(true);
		String op = request.getParameter("method");
		if(op.equals("select"))
		{
			if (!ValidateNull(quick)) {
				if(session.getAttribute("quick")!=null)
					quick = (Quick)session.getAttribute("quick");
			} else {
				session.removeAttribute("quick");
				session.setAttribute("quick", quick);
			}
			model.addAttribute("ptmeth", "select");
		}
		else
		{
			model.addAttribute("ptmeth", "list");
		}
		if(null != quick && null != quick.getSatelliteid())
		{
			String[] sensorlist = SystemConfigUtil.getSystemValue(quick.getSatelliteid(), this.getServletContext());
			model.addAttribute("sensoridlist",sensorlist);
		}
		
		if(page==null)
		{
			quicklist = qmgr.getQuickListByPage(quick,0,Constants.pagesize);
		}
		else
		{
			quicklist = qmgr.getQuickListByPage(quick,(Integer.parseInt(page)-1)*Constants.pagesize,Constants.pagesize);
		}
		
		for (int i = 0; i < quicklist.size(); i++) {
			Map<String,String> browermap = new HashMap<String, String>();
			Quick q = quicklist.get(i);
			String browerstrs = q.getBrowsearchivedir();//快视图路径，多个路径
			if(browerstrs!=null && !browerstrs.isEmpty())
			{
				String[] browerlist = browerstrs.split(",");//解析快视图路径
				for (int j = 0; j < browerlist.length; j++) {
					String browser = browerlist[j];
					String[] sensors = browser.split("-");
					if(sensors.length>3)
					{
						String sensor = sensors[2];
						browermap.put(sensor, browser);
					}
				}
			}
			quicklist.get(i).setViewstring(browermap);
		}
		int resultSize = qmgr.getQuickCount(quick);
		model.addAttribute("resultSize",resultSize);
		model.addAttribute("pagesize",Constants.pagesize);
		String[] satellites = SystemConfigUtil.getSystemValue("satellite", this.getServletContext());
		model.addAttribute("satelliteids",satellites);
		model.addAttribute("quick",quick);
		double size = resultSize;
		model.addAttribute("displayallpage", Math.ceil(size / Constants.pagesize));
		model.addAttribute("thispage", Integer.parseInt(page));
		return new ModelAndView("quick/quicklist","quicklist",quicklist);
	}
	
	//删除快试图信息
	@RequestMapping("/quick/deletequick.html")
	public String deleteQuick(HttpServletRequest request)
	{
		String id = request.getParameter("id");
		Quick quick = null;
		if(id!=null && !id.isEmpty())
			quick = qmgr.getQuickById(Integer.parseInt(id));
		String page = request.getParameter("tz");
		String pagename = DisplayGetPage.getPageName("ordermodels", request);
		String postmethod = request.getParameter("ptmeth");
		if ("".equalsIgnoreCase(page)) {
			page = "1";
		}
		
		if(quick!=null)
		{
			qmgr.deleteQuick(quick);
		}
		if ("select".equalsIgnoreCase(postmethod)) {
			return "redirect:/quick/queryquick.html?method=select&"
					+ pagename + "=" + page;
		} else {
			return "redirect:/quick/queryquick.html?method=list&"
					+ pagename + "=" + page;
		}
	}
	
	//查看快试图信息
	@RequestMapping("/quick/showquick.html")
	public String showQuick(ModelMap map,HttpServletRequest request,HttpServletResponse response)
	{
		String datasetid = request.getParameter("datasetid");
		Quick quick = null;
		if(datasetid!=null && !datasetid.isEmpty())
		{
			quick = qmgr.getQuickById(Integer.parseInt(datasetid));
		}
		map.addAttribute("quick",quick);
		if(request.getParameter("operate").equalsIgnoreCase("show"))
		{
			return "quick/quickview";
		}
		else
		{
			return "quick/quicklook";
		}
	}
	
	//预览快试图信息
	@RequestMapping("/lookquick.html")
	public String lookQuick(ModelMap map,HttpServletRequest request,HttpServletResponse response)
	{
		String sensor = request.getParameter("sensor");
		String datasetid = request.getParameter("datasetid");
		Quick quick = null;
		if(datasetid!=null && !datasetid.isEmpty())
		{
			quick = qmgr.getQuickById(Integer.parseInt(datasetid));
			String browse = quick.getBrowsearchivedir();
			if(browse!=null && !browse.isEmpty())
			{
				String[] browselist = browse.split(",");
				for (int i = 0; i < browselist.length; i++) {
					String browsepath = browselist[i];
					if(browsepath.indexOf(sensor)!=-1)
					{
						quick.setBrowsePath(browsepath);
						break;
					}
				}
			}
		}
		
		map.addAttribute("quick",quick);
		if(request.getParameter("operate").equalsIgnoreCase("show"))
		{
			return "quick/quickview";
		}
		else
		{
			return "quick/quicklook";
		}
	}
	
	private boolean ValidateNull(Quick q) {
		if (q.getSatelliteid()!=null || q.getSensorid()!=null) {
			return true;
		} else {
			return false;
		}
	}
}
